package com.thinkgem.jeesite.modules.sys.service;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.modules.product.entity.attribute.TbProductAttribute;
import com.thinkgem.jeesite.modules.sys.dao.CheckAllDao;
import com.thinkgem.jeesite.modules.sys.dao.TbAgentProductMapper;
import com.thinkgem.jeesite.modules.sys.dao.TbAttachmentMapper;
import com.thinkgem.jeesite.modules.sys.dao.TbProductMapper;
import com.thinkgem.jeesite.modules.sys.entity.TbAgentProduct;
import com.thinkgem.jeesite.modules.sys.entity.TbOrder;
import com.thinkgem.jeesite.modules.sys.entity.TbProduct;
import com.thinkgem.jeesite.modules.sys.entity.TbUserTenementInfo;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by jyy on 2016/10/19 0019.
 */
@Service
public class CheckAllService {

    @Autowired
    private CheckAllDao checkAllDao;
    @Autowired
    private TbProductMapper tbProductMapper;
    @Autowired
    private TbAgentProductMapper tbAgentProductMapper;

    //查询所有订单信息
    @Transactional(readOnly = false)
    public Page<TbOrder> findTbOrderByPage(Page<TbOrder> tbOrderPage, TbOrder tbOrder) {
        tbOrder.setPage(tbOrderPage);
        Date date=tbOrder.getEndDate();
        if(date!=null){
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
        }
        List list = checkAllDao.findAllOrderList(tbOrder);
        tbOrderPage.setList(list);
        return tbOrderPage;
    }
    //查询所有物业缴费信息
    @Transactional(readOnly = true)
    public Page<TbUserTenementInfo> findUserTenementInfoPage(Page<TbUserTenementInfo> page, TbUserTenementInfo tbUserTenementInfo) {
        // 设置默认时间范围，默认当前月
        // 设置默认时间范围，默认当前月
       /* if (tbUserTenementInfo.getBeginDate() == null){
            tbUserTenementInfo.setBeginDate(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
        }
        if (tbUserTenementInfo.getEndDate() == null){
            tbUserTenementInfo.setEndDate(DateUtils.addMonths(tbUserTenementInfo.getBeginDate(), 1));
        }*/
        Date date=tbUserTenementInfo.getEndDate();
        if(date!=null){
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
        }
        tbUserTenementInfo.setPage(page);
        page.setList(checkAllDao.findTenementList(tbUserTenementInfo));
        return page;

    }

    @Transactional(readOnly = true)
    public  Page<TbUserTenementInfo> findPayInfoList(Page<TbUserTenementInfo> tbUserTenementInfoPage, TbUserTenementInfo tbUserTenementInfo){
        tbUserTenementInfo.setPage(tbUserTenementInfoPage);
       /* if (tbUserTenementInfo.getBeginDate() == null){
            tbUserTenementInfo.setBeginDate(DateUtils.setDays(DateUtils.parseDate(DateUtils.getDate()), 1));
        }
        if (tbUserTenementInfo.getEndDate() == null){
            tbUserTenementInfo.setEndDate(DateUtils.addMonths(tbUserTenementInfo.getBeginDate(), 1));
        }*/
        Date date=tbUserTenementInfo.getEndDate();
        if(date!=null){
            date.setHours(23);
            date.setMinutes(59);
            date.setSeconds(59);
        }
        List<TbUserTenementInfo> list =checkAllDao.findPayInfoList(tbUserTenementInfo);
        for(TbUserTenementInfo tbUserTeneInfo:list) {
            BigDecimal totalFee = new BigDecimal(tbUserTeneInfo.getCoveredArea()).multiply(tbUserTeneInfo.getPropertyFee());
            totalFee = totalFee.multiply(new BigDecimal(tbUserTeneInfo.getMonth())).setScale(2);
            tbUserTeneInfo.setTeneFee(totalFee);
        }
        tbUserTenementInfoPage.setList(list);
        return tbUserTenementInfoPage;
    }
    public Page<TbAgentProduct> findProductByPage(Page<TbAgentProduct> tbProductPage, TbAgentProduct tbAgentProduct) {
        tbAgentProduct.setPage(tbProductPage);
        //商品管理list
        List list = checkAllDao.findByPage(tbAgentProduct);
        //根据商品id得到属性
        tbProductPage.setList(list);
        return tbProductPage;
    }
}
